﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Battle;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant.battle;

namespace Pixysoft.Weblications.Sango.Command.pvp_resource
{
    partial class SangoPvpResourceHandler
    {
        public pvpresource_fight pvpresource_fight(long positionindex)
        {
            string userCode = SangoManager.Authentication();

            pvpresource_fight response = ReflectionManager.CreatePojo<pvpresource_fight>();
            response.occupy = false;
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Section section = UsrSectionDao.GetSection(userCode);
            Entities.Usr_Activity activity = UsrActivityDao.getHistory(userCode);


            if (activity.Freebattlecount <= 0 && !StageFormular.isFreeAttackResource(activity.Freebattletypecode))
            {
                response.Message = "无行动力";
                return response;
            }

            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(
                userCode, CtiQueueTypecodeCollection.BATTLE, userCode, BattleFormular.getBattleCooldownTimespan(activity));

            if (queue == null)
            {
                response.Message = "行动力冷却中";
                return response;
            }

            List<Entities.Usr_Resource> myresources = UsrResourceDao.GetUserResources(userCode);

            Entities.Usr_Mission mission = MissionDao.getMission(userCode);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                foreach (Entities.Usr_Resource myresource in myresources.ToArray())
                {
                    if (ResourceFormular.resourceNeedBalance(myresource))
                    {
                        ResourceFormular.resourceCurrentBalance(myresource, profile);
                        command.Update(myresource);
                        myresources.Remove(myresource);
                    }
                }
                if (myresources.Count >= ResourceFormular.GetMaxOccupyResourceCount(profile))
                {
                    command.Update(profile);
                    command.Commit();

                    response.Succeed = true;
                    response.profile = UsrProfileFacade.ToProfile(profile);
                    response.Message = "你可占用的资源数目达到最大值";
                    return;
                }

                Entities.Usr_Resource resource = UsrResourceDao.TryGetOccupyResource(
                    section.Sectionserialcode, positionindex);

                Entities.Cti_Resource sysresource = CtiResourceHelper.searchByPosition(
                    section.Scaletypecode, positionindex);

                // balance

                if (resource != null && ResourceFormular.resourceNeedBalance(resource))
                {
                    Entities.Usr_Profile targetprofile = UsrProfileDao.getUsrProfile(resource.Usercode);
                    ResourceFormular.resourceCurrentBalance(resource, targetprofile);
                    command.Update(resource);
                    command.Update(targetprofile);
                    resource = null;
                }

                if (resource != null)
                {
                    command.Update(profile);
                    command.Commit();

                    response.Succeed = true;
                    response.profile = UsrProfileFacade.ToProfile(profile);
                    response.Message = "目标资源已经被占用";
                    return;
                }

                // fight

                PvxBattleHistory history = PveAttack(command, sysresource, profile);

                // user resource

                if (history.BattleResult == BattleResultCollection.ATTACKWIN)
                {
                    resource = UsrResourceDao.createResource(profile, section, sysresource);
                    command.Insert(resource);

                    ResourceFormular.resourceFightBonus(resource, profile);
                }

                // action point

                if (StageFormular.isFreeAttackResource(activity.Freebattletypecode))
                {
                    activity.Freebattletypecode = (activity.Freebattletypecode - FreeBattleTypecodeCollection.ATTACK_RESOURCE);
                }
                else
                {
                    activity.Freebattlecount = (activity.Freebattlecount - 1);
                    activity.Usedbattlecount = (activity.Usedbattlecount + 1);
                }

                // response

                command.Update(profile);

                command.Update(activity);

                if (MissionDao.processDailyMission(mission, MissionDailyCollection.ATTACK_RESOURCE))
                {
                    command.Update(mission);
                }

                command.Commit();


                response.Succeed = true;
                response.profile = UsrProfileFacade.ToProfile(profile);
                response.activity = UsrProfileFacade.ToActivity(profile, activity);
                response.queue = UsrQueueFacade.ToQueue(queue);
            });

            return response;
        }

        private PvxBattleHistory PveAttack(IEntityNoebeTransaction command,
            Entities.Cti_Resource sysresource, Entities.Usr_Profile profile)
        {
            PvxBattleHistory history = new PvxBattleHistory("", -1, -1);
            history.BattleResult = BattleResultCollection.ATTACKWIN;
            return history;
        }


    }
    public interface pvpresource_fight : ISangoResponse
    {
        /// <summary>
        /// 是否成功占用
        /// </summary>
        bool occupy { get;set;}

        /// <summary>
        /// 玩家信息
        /// </summary>
        player_profile profile { get;set;}

        player_activity activity { get;set;}

        player_queue queue { get;set;}
    }
}
